home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / TimePrefs.lha / TimePrefs / TimePrefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-01  |  12.3 KB  |  520 lines

  1. /*
  2.  * TimePrefs v1.0 - Time Preferences v1.0 ©1998 by Rod Schnell
  3.  *
  4.  *                             using ClassAct GUI.
  5.  *
  6.  *                  also requires classes/gadgets/calendar.gadget
  7.  *
  8.  *
  9.  * What is it?    TimePrefs is a replacement for Sys:Prefs/Time, used to
  10.  *                the system date and time.
  11.  *
  12.  *
  13.  * Why write it?  To kill a few hours, try a few things and because we can
  14.  *                allways use some more simple, yet usefull example sources
  15.  *                using ClassAct. 
  16.  *
  17.  *   History
  18.  *   ~~~~~~~
  19.  *   21-Jun-98 - Released for comments on IRC.
  20.  *
  21.  *   31-Aug-98 - Fixed 128 byte memory leak... Forgot to FreeChooserLabels()
  22.  *               Oops :)
  23.  *             - Minor changes to scroller values, see source comments.
  24.  *             - First public release.
  25.  *
  26.  */
  27.  
  28. /* system includes  */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <time.h>
  34.  
  35. #include <exec/types.h>
  36. #include <exec/memory.h>
  37. #include <intuition/intuition.h>
  38. #include <intuition/gadgetclass.h>
  39. #include <graphics/gfxbase.h>
  40. #include <graphics/text.h>
  41. #include <graphics/gfxmacros.h>
  42. #include <workbench/startup.h>
  43. #include <workbench/workbench.h>
  44.  
  45. #include <proto/intuition.h>
  46. #include <proto/graphics.h>
  47. #include <proto/exec.h>
  48. #include <proto/dos.h>
  49. #include <proto/utility.h>
  50. #include <proto/wb.h>
  51. #include <proto/icon.h>
  52.  
  53. #include <resources/battclock.h>
  54. #include <proto/battclock.h>
  55.  
  56. /* ClassAct includes  */
  57.  
  58. #include <classact.h>
  59.  
  60. /* other gadget includes */
  61.  
  62. #include <gadgets/calendar.h>
  63.  
  64. char vers[] = "\0$VER: TimePrefs v1.0 "__AMIGADATE__"";
  65. char scrtitle[] = "Time Preferences v1.0 ©1998 by Rod Schnell";
  66.  
  67. enum
  68. {
  69.     GID_MAIN=0,
  70.     GID_YEAR,
  71.     GID_MONTH,
  72.     GID_CALENDAR,
  73.     GID_HOURS,
  74.     GID_MINUTES,
  75.     GID_SECONDS,
  76.     GID_TIME,
  77.     GID_SAVE,
  78.     GID_USE,
  79.     GID_NETSYNC,
  80.     GID_CANCEL,
  81.     GID_LAST
  82. };
  83.  
  84. enum
  85. {
  86.     WID_MAIN=0,
  87.     WID_LAST
  88. };
  89.  
  90. enum
  91. {
  92.     OID_MAIN=0,
  93.     OID_LAST
  94. };
  95.  
  96.  
  97. /* function protos */
  98.  
  99. struct ClassLibrary *openclass (STRPTR name, ULONG version);
  100. void maketime( char *buf, struct tm *thetime, struct ClockData *thedate);
  101. void savetime(ULONG t);
  102. void usetime(ULONG t);
  103.  
  104.  
  105. int main(void)
  106. {
  107.   struct ClassLibrary *CalendarBase = NULL;
  108.  
  109.     struct MsgPort *AppPort;
  110.     struct Window *windows[WID_LAST];
  111.  
  112.     Object *objects[OID_LAST];
  113.     struct Gadget *gadgets[GID_LAST];
  114.  
  115.     struct List *monthlist = NULL;
  116.  
  117.     ULONG seconds, micros;
  118.     struct ClockData thedate;
  119.  
  120.     UBYTE timebuffer[256];
  121.     struct tm thetime;
  122.  
  123.     ULONG n;
  124.  
  125.     if (!(CalendarBase = openclass ("gadgets/calendar.gadget", 37)))
  126.         return(30);
  127.  
  128.     /* make sure our classes opened... */
  129.  
  130.     if (!ButtonBase || !IntegerBase || !ChooserBase || !WindowBase || !LayoutBase)
  131.     {
  132.         CloseLibrary ((struct Library *) CalendarBase);
  133.         return(30);
  134.     }
  135.     else if ( AppPort = CreateMsgPort() )
  136.     {
  137.         monthlist = ChooserLabels( "January","February","March","April","May","June", "July", "August", "September", "October", "November", "December", NULL );
  138.          CurrentTime( &seconds, µs);
  139.          
  140.          /* need struct ClockData thedate for calendar.gadget */
  141.  
  142.         Amiga2Date( seconds,  &thedate );
  143.         
  144.         maketime(timebuffer, &thetime, &thedate);
  145.  
  146.         /* Create the window object.
  147.          */
  148.         objects[OID_MAIN] = WindowObject,
  149.             WA_ScreenTitle, scrtitle,
  150.             WA_Title, "Time Preferences",
  151.             WA_Activate, TRUE,
  152.             WA_DepthGadget, TRUE,
  153.             WA_DragBar, TRUE,
  154.             WA_CloseGadget, TRUE,
  155.             WA_SizeGadget, TRUE,
  156.             WA_SizeBBottom,    TRUE,
  157.             WINDOW_IconifyGadget, TRUE,
  158.             WINDOW_IconTitle, "TimePrefs",
  159.             WINDOW_AppPort, AppPort,
  160.             WINDOW_Position, WPOS_CENTERMOUSE,
  161.             WA_IDCMP, IDCMP_GADGETUP,
  162.             WINDOW_ParentGroup, gadgets[GID_MAIN] = VGroupObject,
  163.                 LAYOUT_SpaceOuter, TRUE,
  164.                 LAYOUT_DeferLayout, TRUE,
  165.  
  166.                 StartHGroup,
  167.  
  168.                     LAYOUT_AddChild, gadgets[GID_YEAR] = IntegerObject,
  169.                         GA_ID, GID_YEAR,
  170.                         GA_RelVerify, TRUE,
  171.                         INTEGER_Arrows, TRUE,
  172.                         INTEGER_MaxChars, 4,
  173.                         INTEGER_Minimum, 1978,
  174.                         INTEGER_Maximum, 3000,
  175.                         INTEGER_Number, thedate.year,
  176.                     IntegerEnd,
  177.                     CHILD_WeightedHeight, 0,
  178.  
  179.                     LAYOUT_AddChild, gadgets[GID_MONTH] = ChooserObject,
  180.                         GA_ID, GID_MONTH,
  181.                         GA_RelVerify, TRUE,
  182.                         CHOOSER_Labels, monthlist,
  183.                         CHOOSER_Selected, thedate.month-1, /* MONTH # - 1 */
  184.                     ChooserEnd,
  185.                     CHILD_WeightedHeight, 0,
  186.  
  187.                 EndHGroup,
  188.                 CHILD_WeightedHeight, 0,
  189.  
  190.                 LAYOUT_AddChild, gadgets[GID_CALENDAR] = CalendarObject,
  191.                     GA_ID, GID_CALENDAR,
  192.                     GA_RelVerify, TRUE,
  193.                     GA_Immediate,        TRUE,
  194.                     CALENDAR_ClockData, &thedate,
  195.                 CalendarEnd,
  196.  
  197.                 StartVGroup, EvenSized,
  198.  
  199.                     LAYOUT_AddChild, gadgets[GID_HOURS] = ScrollerObject,
  200.                         GA_ID, GID_HOURS,
  201.                         GA_RelVerify, TRUE,
  202.                         SCROLLER_Total, 24, /* 0-23 */
  203.                         SCROLLER_Arrows, TRUE,
  204.                         SCROLLER_ArrowDelta, (WORD)1,
  205.                         SCROLLER_Orientation, SORIENT_HORIZ,
  206.                         SCROLLER_Visible, 2,
  207.                         SCROLLER_Top, thedate.hour,
  208.                     ScrollerEnd,
  209.                     CHILD_Label, LabelObject, LABEL_Text, "Hours", LabelEnd,
  210.                     CHILD_WeightedHeight, 0,
  211.  
  212.                     LAYOUT_AddChild, gadgets[GID_MINUTES] = ScrollerObject,
  213.                         GA_ID, GID_MINUTES,
  214.                         GA_RelVerify, TRUE,
  215.                         SCROLLER_Total, 60, /* 0-59 */
  216.                         SCROLLER_Arrows, TRUE,
  217.                         SCROLLER_ArrowDelta, (WORD)1,
  218.                         SCROLLER_Orientation, SORIENT_HORIZ,
  219.                         SCROLLER_Visible, 5,
  220.                         SCROLLER_Top, thedate.min,
  221.                     ScrollerEnd,
  222.                     CHILD_Label, LabelObject, LABEL_Text, "Minutes", LabelEnd,
  223.                     CHILD_WeightedHeight, 0,
  224.  
  225.                     LAYOUT_AddChild, gadgets[GID_SECONDS] = ScrollerObject,
  226.                         GA_ID, GID_SECONDS,
  227.                         GA_RelVerify, TRUE,
  228.                         SCROLLER_Total, 60, /* 0-59 */
  229.                         SCROLLER_Arrows, TRUE,
  230.                         SCROLLER_ArrowDelta, (WORD)1,
  231.                         SCROLLER_Orientation, SORIENT_HORIZ,
  232.                         SCROLLER_Visible, 5,
  233.                         SCROLLER_Top, thedate.sec,
  234.                     ScrollerEnd,
  235.                     CHILD_Label, LabelObject, LABEL_Text, "Seconds", LabelEnd,
  236.                     CHILD_WeightedHeight, 0,
  237.  
  238.                 EndVGroup,
  239.                 CHILD_WeightedHeight, 0,
  240.  
  241.                 LAYOUT_AddChild, gadgets[GID_TIME] = ButtonObject,
  242.                     GA_ID, GID_TIME,
  243.                     GA_Text, timebuffer,
  244.                     GA_ReadOnly, TRUE,
  245.                     BUTTON_Justification, BCJ_CENTER,
  246.                 ButtonEnd,
  247.                 CHILD_WeightedHeight, 0,
  248.  
  249.                 StartHGroup, EvenSized,
  250.  
  251.                     LAYOUT_AddChild, ButtonObject,
  252.                         GA_ID, GID_SAVE,
  253.                         GA_RelVerify, TRUE,
  254.                         GA_Text,"_Save",
  255.                     ButtonEnd,
  256.                     CHILD_WeightedHeight, 0,
  257.  
  258.                     LAYOUT_AddChild, ButtonObject,
  259.                         GA_ID, GID_USE,
  260.                         GA_RelVerify, TRUE,
  261.                         GA_Text,"_Use",
  262.                     ButtonEnd,
  263.                     CHILD_WeightedHeight, 0,
  264. /*
  265.                     LAYOUT_AddChild, ButtonObject,
  266.                         GA_ID, GID_NETSYNC,
  267.                         GA_Disabled, TRUE,
  268.                         GA_RelVerify, TRUE,
  269.                         GA_Text,"_Net Sync",
  270.                     ButtonEnd,
  271.                     CHILD_WeightedHeight, 0,
  272. */
  273.                     LAYOUT_AddChild, ButtonObject,
  274.                         GA_ID, GID_CANCEL,
  275.                         GA_RelVerify, TRUE,
  276.                         GA_Text,"_Cancel",
  277.                     ButtonEnd,
  278.                     CHILD_WeightedHeight, 0,
  279.                 EndHGroup,
  280.                 CHILD_WeightedHeight, 0,
  281.  
  282.             EndGroup,
  283.         EndWindow;
  284.  
  285.         if (objects[OID_MAIN])
  286.         {
  287.              /*  Object creation was sucessfull, open the window. */
  288.  
  289.             if (windows[WID_MAIN] = (struct Window *) CA_OpenWindow(objects[OID_MAIN]))
  290.             {
  291.                 ULONG wait, signal, app = (1L << AppPort->mp_SigBit);
  292.                 ULONG done = FALSE;
  293.                 ULONG result;
  294.                 UWORD code;
  295.  
  296.                  /* Obtain the window wait signal mask. */
  297.  
  298.                 GetAttr(WINDOW_SigMask, objects[OID_MAIN], &signal);
  299.  
  300.                 while (!done)
  301.                 {
  302.                     wait = Wait( signal | SIGBREAKF_CTRL_C | app );
  303.  
  304.                     if ( wait & SIGBREAKF_CTRL_C )
  305.                     {
  306.                         /* received CTRL-C, time to quit */
  307.  
  308.                         done = TRUE;
  309.                     }
  310.                     else
  311.                     {
  312.                         while ( (result = CA_HandleInput(objects[OID_MAIN], &code) ) != WMHI_LASTMSG )
  313.                         {
  314.                             switch (result & WMHI_CLASSMASK)
  315.                             {
  316.                                 case WMHI_CLOSEWINDOW:
  317.                                     windows[WID_MAIN] = NULL;
  318.                                     done = TRUE;
  319.                                     break;
  320.  
  321.                                 case WMHI_GADGETUP:
  322.                                     switch (result & WMHI_GADGETMASK)
  323.                                     {
  324.                                         case GID_YEAR:
  325.                                             GetAttr(INTEGER_Number, gadgets[GID_YEAR], &n);
  326.                                             thedate.year = n;
  327.                                             break;
  328.  
  329.                                         case GID_MONTH:
  330.                                             GetAttr(CHOOSER_Active, gadgets[GID_MONTH], &n);
  331.                                             thedate.month = n+1;
  332.                                             break;
  333.  
  334.                                         case GID_CALENDAR:
  335.                                             thedate.mday = code;
  336.                                             break;
  337.  
  338.                                         case GID_HOURS:
  339. /* clicking scroller (not arrows) changes SCROLLER_Top by SCROLLER_Visible - 1 */
  340. /* clicking arrows changes SCROLLER_Top by the value of SCROLLER_ArrowDelta    */
  341.                                             thedate.hour = code;
  342.                                             break;
  343.  
  344.                                         case GID_MINUTES:
  345.                                             thedate.min = code;
  346.                                             break;
  347.  
  348.                                         case GID_SECONDS:
  349.                                             thedate.sec = code;
  350.                                             break;
  351.  
  352.                                         case GID_SAVE:
  353.                                             savetime( Date2Amiga( &thedate ) );
  354.                                             done = TRUE;
  355.                                             break;
  356.  
  357.                                         case GID_USE:
  358.                                             usetime( Date2Amiga( &thedate ) );
  359.                                             done = TRUE;
  360.                                             break;
  361.  
  362.                                         case GID_NETSYNC:
  363.                                             done = TRUE;
  364.                                             break;
  365.  
  366.                                         case GID_CANCEL:
  367.                                             done = TRUE;
  368.                                             break;
  369.                                         
  370.                                         default:
  371.                                             break;
  372.                                     }
  373.                                     switch (result & WMHI_GADGETMASK)
  374.                                     {
  375.                                         /* refresh calendar and date/time as necessary */
  376.  
  377.                                         case GID_SAVE:
  378.                                         case GID_USE:
  379.                                         case GID_NETSYNC:
  380.                                         case GID_CANCEL:
  381.                                             /* Do nothing */
  382.                                             break;
  383.  
  384.                                         case GID_YEAR:
  385.                                         case GID_MONTH:
  386.                                             SetGadgetAttrs(    gadgets[GID_CALENDAR], windows[WID_MAIN], NULL,
  387.                                                                             CALENDAR_ClockData, &thedate,
  388.                                                                             TAG_DONE);
  389.                                             RefreshGList( gadgets[GID_CALENDAR], windows[WID_MAIN], NULL, 1 );
  390.  
  391.                                         /* Fall through and update time string too */
  392.  
  393.                                         default:
  394.                                             maketime(timebuffer, &thetime, &thedate);
  395.                                             SetGadgetAttrs(    gadgets[GID_TIME], windows[WID_MAIN], NULL,
  396.                                                                             GA_Text, timebuffer,
  397.                                                                             TAG_DONE);
  398.                                             RefreshGList( gadgets[GID_TIME], windows[WID_MAIN], NULL, 1 );
  399.                                             break;
  400.                                     }
  401.                                     break;
  402.  
  403.                                 case WMHI_ICONIFY:
  404.                                     CA_Iconify(objects[OID_MAIN]);
  405.                                     windows[WID_MAIN] = NULL;
  406.                                     break;
  407.  
  408.                                 case WMHI_UNICONIFY:
  409.                                     windows[WID_MAIN] = (struct Window *) CA_OpenWindow(objects[OID_MAIN]);
  410.  
  411.                                     if (windows[WID_MAIN])
  412.                                     {
  413.                                         GetAttr(WINDOW_SigMask, objects[OID_MAIN], &signal);
  414.                                     }
  415.                                     else
  416.                                     {
  417.                                         done = TRUE;    // error re-opening window!
  418.                                     }
  419.                                      break;
  420.                             }
  421.                         }
  422.                     }
  423.                 }
  424.             }
  425.  
  426.             /* Disposing of the window object will also close the window if it is
  427.              * already opened, and it will dispose of the layout object attached to it.
  428.              */
  429.             DisposeObject(objects[OID_MAIN]);
  430.         }
  431.  
  432.         if( monthlist) FreeChooserLabels( monthlist );
  433.         DeleteMsgPort(AppPort);
  434.     }
  435.     if( CalendarBase ) CloseLibrary ((struct Library *) CalendarBase);
  436.  
  437.     return(0);
  438. }
  439.  
  440.  
  441. /* Try opening the class library from a number of common places */
  442.  
  443. struct ClassLibrary *openclass (STRPTR name, ULONG version)
  444. {
  445.     struct Library *retval;
  446.     UBYTE buffer[256];
  447.  
  448.     if ((retval = OpenLibrary (name, version)) == NULL)
  449.     {
  450.         sprintf (buffer, ":classes/%s", name);
  451.         if ((retval = OpenLibrary (buffer, version)) == NULL)
  452.         {
  453.             sprintf (buffer, "classes/%s", name);
  454.             retval = OpenLibrary (buffer, version);
  455.         }
  456.     }
  457.     return (struct ClassLibrary *) retval;
  458. }
  459.  
  460.  
  461. /* convert struct ClockData into struct tm and format string */
  462.  
  463. void maketime( char *buf, struct tm *thetime, struct ClockData *thedate){
  464.     thetime->tm_sec  = thedate->sec;
  465.     thetime->tm_min  = thedate->min;
  466.     thetime->tm_hour = thedate->hour;
  467.     thetime->tm_mday = thedate->mday;
  468.     thetime->tm_mon  = thedate->month-1;
  469.     thetime->tm_year = thedate->year-1900;
  470.     thetime->tm_wday = thedate->wday;
  471.  
  472.     strftime(buf, 255, "%c", thetime);
  473. }
  474.  
  475.  
  476. /* Use time/date and save it to the battery backed clock too */
  477.  
  478. void savetime(ULONG t){
  479.     struct Library *BattClockBase;
  480.  
  481.     if (BattClockBase= OpenResource(BATTCLOCKNAME))
  482.     {
  483.         WriteBattClock(t);
  484.     }
  485.     usetime( t );
  486. }
  487.  
  488.  
  489. /* Use time/date, but do not save to battery backed clock */
  490.  
  491. void usetime(ULONG t){
  492.     struct timerequest *TimerIO;
  493.     struct MsgPort *TimerMP;
  494.  
  495.     LONG error;
  496.  
  497.     if (TimerMP = CreatePort(0,0))
  498.     {
  499.         if (TimerIO = (struct timerequest *) CreateExtIO(TimerMP,sizeof(struct timerequest)) )
  500.         {
  501.             /* Open with UNIT_VBLANK, but any unit can be used */
  502.  
  503.             if (!(error=OpenDevice(TIMERNAME,UNIT_VBLANK,(struct IORequest *)TimerIO,0L)))
  504.             {
  505.                 TimerIO->tr_time.tv_micro = 0;
  506.                 TimerIO->tr_time.tv_secs = t;
  507.  
  508.                 /* Issue the command, wait for it to finish, then get the reply */
  509.  
  510.                 TimerIO->tr_node.io_Command = TR_SETSYSTIME;
  511.                 DoIO((struct IORequest *) TimerIO);
  512.                 CloseDevice((struct IORequest *) TimerIO);
  513.             }
  514.             DeleteExtIO((struct IORequest *)TimerIO);
  515.         }
  516.         DeletePort(TimerMP);
  517.     }
  518. }
  519.  
  520.